/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Invite;
import org.cx1.model.User;
import org.cx1.model.utils.EmailInvitee;
import org.cx1.model.utils.Invitee;
import org.cx1.model.utils.MobileInvitee;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IInviteService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DInviteService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MEmailService;
import org.cx1.service.impl.MInviteService;
import org.cx1.service.impl.MUserService;
import org.cx1.web.controller.action.InviteAction;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class InviteController extends BaseController {
    
    //private IUserService userService = new MUserService();
    
    private IUserService userService = new DUserService();
    private IInviteService dInviteService = new DInviteService();
    
    private IInviteService inviteService = new MInviteService();
    private IEmailService emailService = new MEmailService();
    
    public InviteController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        boolean loggedin = false;
        String userID = SessionUtils.getUserID(request.getSession());   
        
        if (!StringUtils.isEmpty(userID)) {
            loggedin = true;
            map.put("loggedin", loggedin);
        }
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case InviteAction.CREATE: {          
                create(mav, map, request);
                break;
            }
            case InviteAction.READ_ALL: {                
                if (loggedin) {
                    readAll(mav, map, request);               
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.SEND_INVITE: {               
                if (loggedin) {
                    sendInvite(mav, map, request);           
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;   
            }
            case InviteAction.INVITE_SUCCESS: {
                inviteSuccess(mav, map, request);
                break;
            }
            case InviteAction.SEND_POST_LISTING_REMINDER: {               
                if (loggedin) {
                    sendPostListingReminder(mav, map, request);          
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.READ_ALL_INVITEE: {        
                if (loggedin) {
                    readAllInvitee(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.CREATE_INVITEE: {
                if (loggedin) {
                    createInvitee(mav, map, request);    
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.DELETE_INVITEE: {
                if (loggedin) {
                    deleteInvitee(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.DELETE_EXISTING_USERS_IN_EMAIL_INVITE_LIST: {
                if (loggedin) {
                    deleteExistingUsersInEmailInviteList(mav, map, request);     
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.READ_ALL_EMAIL_INVITEE: {        
                if (loggedin) {
                    readAllEmailInvitee(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.READ_ALL_EMAIL_INVITEE_BY_PROCESSED: {        
                if (loggedin) {
                    readAllEmailInviteeByProcessed(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.CREATE_EMAIL_INVITEE: {
                if (loggedin) {
                    createEmailInvitee(mav, map, request);    
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.DELETE_EMAIL_INVITEE: {
                if (loggedin) {
                    deleteEmailInvitee(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.DELETE_EMAIL_INVITEES: {
                if (loggedin) {
                    deleteEmailInvitees(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.SET_EMAIL_INVITEE_PROCESSED: {
                if (loggedin) {
                    setEmailInviteeProcessed(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
                
            case InviteAction.READ_ALL_MOBILE_INVITEE: {        
                if (loggedin) {
                    readAllMobileInvitee(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.READ_ALL_MOBILE_INVITEE_BY_PROCESSED: {        
                if (loggedin) {
                    readAllMobileInviteeByProcessed(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.CREATE_MOBILE_INVITEE: {
                if (loggedin) {
                    createMobileInvitee(mav, map, request);    
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.DELETE_MOBILE_INVITEE: {
                if (loggedin) {
                    deleteMobileInvitee(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.SET_MOBILE_INVITEE_PROCESSED: {
                if (loggedin) {
                    setMobileInviteeProcessed(mav, map, request);   
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case InviteAction.LIKE: {
                like(mav, map, request);   
                break;
            }
            default: {
                create(mav, map, request);
                break;
            }                
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        
        mav.addObject("model", map);
        return mav;
    }
    
    private void readAll(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<Invite>> response = inviteService.readll(0, 0);        
        map.put("invites", response.getResponse());
        mav.setViewName("invites");
    }
    
    private void create(ModelAndView mav, Map map, HttpServletRequest request) { 
        if (isThisRequestCommingFromAMobileDevice(request)) {  
            mav.setViewName("inviteadd_mobile");  
        }
        else {
            mav.setViewName("inviteadd");           
        }
    }
    
    private void sendInvite(ModelAndView mav, Map map, HttpServletRequest request) {  
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String type = request.getParameter("type");
            
            if (!StringUtils.isEmpty(type)) {
                if (Integer.parseInt(type) == 1) {
                    emailService.sendInvite1(name, email);                    
                }
                else if (Integer.parseInt(type) == 2) {
                    emailService.sendInvite2(name, email);                    
                }
            }            
            mav.setView(new RedirectView("invite.htm?a=9000"));
        }
        else {
            mav.setViewName("invitesend");            
        }
    }
            
    private void sendPostListingReminder(ModelAndView mav, Map map, HttpServletRequest request) {  
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String type = request.getParameter("type");
            
            if (!StringUtils.isEmpty(type)) {
                emailService.sendPostListingReminder(name, email);
            }            
            mav.setView(new RedirectView("invite.htm?a=9000"));
        }
        else {
            mav.setViewName("invitesend");            
        }
    }
    
    private void inviteSuccess(ModelAndView mav, Map map, HttpServletRequest request) {  
        mav.setViewName("invitesuccess");
    }
    
    private void readAllInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        ServiceResponse<List<Invitee>> response1 = inviteService.readAllInvitee(0, 0);
        map.put("invitees", response1.getResponse());
        mav.setViewName("invitees");
    }    
    
    private void createInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        
        Invitee invitee = new Invitee();
        invitee.setName(name);
        invitee.setEmail(email);
        invitee.setMobile(mobile);
        
        ServiceResponse<String> response = inviteService.createInvitee(invitee);
        mav.setView(new RedirectView("invite.htm?a=11"));
    }
    
    private void deleteInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String id = request.getParameter("id");        
        ServiceResponse<Boolean> response = inviteService.deleteInvitee(id);
        mav.setView(new RedirectView("invite.htm?a=21"));
    }
        
    private void deleteExistingUsersInEmailInviteList(ModelAndView mav, Map map, HttpServletRequest request) {  
        ServiceResponse<Boolean> response1 = inviteService.deleteExisitingUsersInEmailInviteList();
        mav.setView(new RedirectView("invite.htm?a=21"));
    }
    
    private void readAllEmailInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        ServiceResponse<List<EmailInvitee>> response1 = inviteService.readAllEmailInvitee(0, 0);
        map.put("emailInvitees", response1.getResponse());
        mav.setViewName("emailinvitees");
    }    
    
    private void readAllEmailInviteeByProcessed(ModelAndView mav, Map map, HttpServletRequest request) {  
        String strProcessed = request.getParameter("processed");
        String strOffset = request.getParameter("offset");
        String strLimit = request.getParameter("limit");
        
        boolean processed = StringUtils.isEmpty(strProcessed) ? false : true;
        int offset = StringUtils.isEmpty(strOffset) ? 0 : Integer.parseInt(strOffset);
        int limit = StringUtils.isEmpty(strLimit) ? 100 : Integer.parseInt(strLimit);
                
        ServiceResponse<List<EmailInvitee>> response1 = dInviteService.readEmailInviteesByProcessed(offset, limit, processed);
        map.put("emailInvitees", response1.getResponse());
        mav.setViewName("emailinvitees");
    }    
    
    private void createEmailInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String multiEmail = request.getParameter("multiEmail");
        
        EmailInvitee emailInvitee = new EmailInvitee();
        emailInvitee.setName(name);
        emailInvitee.setEmail(email);        
        ServiceResponse<String> response = dInviteService.createEmailInvitee(emailInvitee);  
        
        if (!StringUtils.isEmpty(multiEmail)) {
            String[] emailArr = multiEmail.split("\n");
            if (emailArr.length > 0) {
                for (int i = 0; i < emailArr.length; i++) {
                    EmailInvitee emailInvitee1 = new EmailInvitee();
                    emailInvitee1.setEmail(emailArr[i]);
                    ServiceResponse<String> response1 = dInviteService.createEmailInvitee(emailInvitee1); 
                }
            }
        }        
        mav.setView(new RedirectView("invite.htm?a=27"));
    }
    
    private void deleteEmailInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String email = request.getParameter("email");        
        ServiceResponse<Boolean> response = dInviteService.deleteEmailInviteeByEmail(email);
        mav.setView(new RedirectView("invite.htm?a=27"));
    }
    
    private void deleteEmailInvitees(ModelAndView mav, Map map, HttpServletRequest request) {  
        String multiEmail = request.getParameter("multiEmail");                
        
        if (!StringUtils.isEmpty(multiEmail)) {
            String[] emailArr = multiEmail.split("\n");
            if (emailArr.length > 0) {
                for (int i = 0; i < emailArr.length; i++) {
                    ServiceResponse<Boolean> response1 = dInviteService.deleteEmailInviteeByEmail(emailArr[i].trim());
                }
            }
        } 
        mav.setView(new RedirectView("invite.htm?a=27"));
    }
    
    private void setEmailInviteeProcessed(ModelAndView mav, Map map, HttpServletRequest request) {  
        String multiEmail = request.getParameter("multiEmail");        
        String strProcessed = request.getParameter("strProcessed"); 
        
        boolean processed = false;
        if (!StringUtils.isEmpty(strProcessed)) {
            processed = true;
        }
        
        if (!StringUtils.isEmpty(multiEmail)) {
            String[] emailArr = multiEmail.split("\n");
            if (emailArr.length > 0) {
                for (int i = 0; i < emailArr.length; i++) {
                    ServiceResponse<Boolean> response1 = dInviteService.setEmailInviteeProcessed(emailArr[i].trim(), processed);
                }
            }
        }      
        mav.setView(new RedirectView("invite.htm?a=27"));
    }
        
    
    private void readAllMobileInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        ServiceResponse<List<MobileInvitee>> response1 = inviteService.readAllMobileInvitee(0, 0);
        map.put("mobileInvitees", response1.getResponse());
        mav.setViewName("mobileinvitees");
    }    
    
    private void readAllMobileInviteeByProcessed(ModelAndView mav, Map map, HttpServletRequest request) {  
        String strProcessed = request.getParameter("processed");
        String strOffset = request.getParameter("offset");
        String strLimit = request.getParameter("limit");
        
        boolean processed = StringUtils.isEmpty(strProcessed) ? false : true;
        int offset = StringUtils.isEmpty(strOffset) ? 0 : Integer.parseInt(strOffset);
        int limit = StringUtils.isEmpty(strLimit) ? 100 : Integer.parseInt(strLimit);
                
        ServiceResponse<List<MobileInvitee>> response1 = dInviteService.readMobileInviteesByProcessed(offset, limit, processed);
        map.put("mobileInvitees", response1.getResponse());
        mav.setViewName("mobileinvitees");
    }    
    
    private void createMobileInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String multiMobile = request.getParameter("multiMobile");
        
        MobileInvitee mobileInvitee = new MobileInvitee();
        mobileInvitee.setName(name);
        mobileInvitee.setMobile(mobile);        
        ServiceResponse<String> response = dInviteService.createMobileInvitee(mobileInvitee);  
        
        if (!StringUtils.isEmpty(multiMobile)) {
            String[] mobileArr = multiMobile.split("\n");
            if (mobileArr.length > 0) {
                for (int i = 0; i < mobileArr.length; i++) {
                    MobileInvitee mobileInvitee1 = new MobileInvitee();
                    mobileInvitee1.setMobile(mobileArr[i]);
                    ServiceResponse<String> response1 = dInviteService.createMobileInvitee(mobileInvitee1); 
                }
            }
        }        
        mav.setView(new RedirectView("invite.htm?a=37"));
    }
    
    private void deleteMobileInvitee(ModelAndView mav, Map map, HttpServletRequest request) {  
        String mobile = request.getParameter("mobile");                
        ServiceResponse<Boolean> response = dInviteService.deleteMobileInviteeByMobile(mobile);
        mav.setView(new RedirectView("invite.htm?a=37"));
    }
        
    private void setMobileInviteeProcessed(ModelAndView mav, Map map, HttpServletRequest request) {  
        String multiMobile = request.getParameter("multiMobile");        
        String strProcessed = request.getParameter("strProcessed"); 
        
        boolean processed = false;
        if (!StringUtils.isEmpty(strProcessed)) {
            processed = true;
        }
        
        if (!StringUtils.isEmpty(multiMobile)) {
            String[] mobileArr = multiMobile.split("\n");
            if (mobileArr.length > 0) {
                for (int i = 0; i < mobileArr.length; i++) {
                    ServiceResponse<Boolean> response1 = dInviteService.setMobileInviteeProcessed(mobileArr[i].trim(), processed);
                }
            }
        }      
        mav.setView(new RedirectView("invite.htm?a=37"));
    }
       
    private void like(ModelAndView mav, Map map, HttpServletRequest request) {  
        mav.setViewName("like");
    }
    
    
}